package com.ruoxi.blog.test;

/**
	* @author YaoXian
	* @title: ListNode
	* @projectName ruoxi-blog
	* @description: 若兮微服务 -
	* @date 2021-03-18 22:34
	*/

public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
		
		public static void main(String[] args) {
				ListNode head = new ListNode(1);
				for (int i = 2; i < 3; i++) {
						Solution.add(head,i);
				}
				ListNode listNode = Solution.swapPairs(head);
				System.out.println(listNode.next.val);
				Solution.Li(listNode);
				
		}
}

class Solution {
		
		public static ListNode swapPairs(ListNode head) {
				if(head == null) return null;
				if(head.next == null) return head;
				ListNode before = head;ListNode after = head.next;ListNode temp = head;
				while(true){
						before.next = after.next;
						after.next = before;
						if(temp.next == null || temp.next.next == null){
								break;
						}
						temp = temp.next.next;
						before = temp;
						after = temp.next;
				}
				return head;
		}
		
		public static ListNode removeNthFromEnd(ListNode head, int n) {
				ListNode slow = head.next;
				ListNode temp = head;
				ListNode quick = head.next;int count = 0;
				if(head.next == null){
						return null;
				}
				while(true){
						if(quick == null){
								temp.next = slow.next;
								break;
						}
						if(count >= n){
								slow = slow.next;
								temp = temp.next;
						}
						quick = quick.next;
						count += 1;
				}
				return head;
		}
		
		public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
				ListNode returnHead = new ListNode();
				ListNode assist = new ListNode();
				int i = 0;
				int sum;
				int temp;
				int count = 0;
				int length = addLinked(l1,l2);
				System.out.println(length);
				int [] arr = new int[length+1];
				ListNode tempNode1 = l1;
				ListNode tempNode2 = l2;
				while(length + 1> i){
						ListNode newNode = new ListNode();
						sum = tempNode1.val + tempNode2.val;
						tempNode1 = tempNode1.next;
						tempNode2 = tempNode2.next;
						if(sum < 10){
								if(count == 0){
										arr[i] = sum;
								}else{
										if(sum + 1 == 10){
												arr[i] = 0;
										}else{
												arr[i] = sum + 1;
												count = 0;
										}
								}
						}else{
								temp = sum - 10;
								if(count == 0){
										arr[i] = temp;
								}else{
										arr[i] = temp+1;
								}
								count = 1;
						}
						if(i == 0){
								returnHead.val = arr[i];
								assist = returnHead;
						}else{
								newNode.val = arr[i];
								assist.next = newNode;
								assist = assist.next;
						}
						i++;
						System.out.println(count);
						if(length == i && count == 1){
								assist.next = new ListNode(1);
						}
				}
				return returnHead;
		}
		
		public static void Li(ListNode l1){
				ListNode temp = l1;
				while(true){
						if(temp.next == null){
								break;
						}
						System.out.print(temp.val + " ");
						temp = temp.next;
				}
		}
		
		public static int addLinked(ListNode l1, ListNode l2){
				int l1Length = linkedLength(l1);
				int l2Length = linkedLength(l2);
				if(l1Length > l2Length){
						for(int i = l2Length;i < l1Length;i++){
								add(l2,0);
						}
						return l1Length;
				}else if(l1Length < l2Length){
						for(int i = l1Length;i < l2Length;i++){
								add(l1,0);
						}
						return l2Length;
				}
				return l1Length;
		}
		
		public static void add(ListNode l1,int n){
				ListNode temp = l1;
				while(true){
						if(temp.next == null){
								temp.next = new ListNode(n);
								break;
						}
						temp = temp.next;
				}
		}
		
		
		public static int linkedLength(ListNode l1){
				int count = 0;
				ListNode temp = l1;
				while(true){
						if(temp.next == null){
								break;
						}
						count++;
						temp = temp.next;
				}
				return count;
		}
		
		public static ListNode delete(ListNode head) {
				ListNode temp2 = new ListNode(0,head);
				ListNode temp1 = temp2;
				//[1,2,4,4,5]
				while (temp1.next != null && temp1.next.next != null) {
						if (temp1.next.val == temp1.next.next.val){
								int x = temp1.next.val;
								while (temp1.next != null && temp1.next.val == x){
										temp1.next = temp1.next.next;
								}
						} else{
								temp1 = temp1.next;
						}
				}
				return temp2.next;
		}
		
		public static ListNode oddEvenList(ListNode head) {
				ListNode tempA = head;//快指针 每次走两步
				ListNode tempB = head;//慢指针 每次走一步
				int temp;
				while (tempA.next !=null && tempA.next.next!=null){
						tempB = tempB.next;
						tempA = tempA.next.next;
						temp = tempA.val;
						tempA.val = tempB.val;
						tempB.val = temp;
				}
				return head;
		}
		
}